package com.wondertek.web.controller.sys;

import com.wondertek.common.base.Result;
import com.wondertek.service.dto.sys.req.SysJobAddReq;
import com.wondertek.service.dto.sys.req.SysJobUpdateReq;
import com.wondertek.service.service.sys.ISysJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务管理
 */
@RestController
@RequestMapping("/backend/sys/schedule")
@Tag(name = "定时任务管理")
public class SysJobController {

    @Resource
    private ISysJobService sysJobService;

    /**
     * 新增定时任务
     */
    @PostMapping("/add")
    @Operation(summary = "新增定时任务")
    public Result<?> save(@RequestBody @Validated SysJobAddReq sysJobAddReq) {
        sysJobService.add(sysJobAddReq);
        return Result.success();
    }

    /**
     * 修改定时任务
     */
    @PostMapping("/update")
    @Operation(summary = "修改定时任务")
    public Result<?> update(@RequestBody @Validated SysJobUpdateReq sysJobUpdateReq) {
        sysJobService.updateSysJob(sysJobUpdateReq);
        return Result.success();
    }

    /**
     * 删除定时任务
     */
    @PostMapping(value = "/del")
    @Operation(summary = "删除定时任务")
    public Result<?> del(@RequestParam("id") String id) {
        sysJobService.removeJobById(id);
        return Result.success();
    }

    /**
     * 立即执行任务
     */
    @GetMapping(value = "/run")
    @Operation(summary = "立即执行任务")
    public Result<?> run(@RequestParam("id") String id) {
        sysJobService.run(id);
        return Result.success();
    }

    /**
     * 强制重建
     *
     * @param id
     * @return
     */
    @GetMapping("/foredCreation")
    @Operation(summary = "强制重建")
    public Result<?> foredCreation(@RequestParam("id") String id) {
        sysJobService.foredCreation(id);
        return Result.success();
    }

    @GetMapping("/isCheckExistsJob")
    @Operation(summary = "判断任务是否存在")
    public Result<Boolean> isCheckExistsJob(@RequestParam("id") String id) {
        return Result.success(sysJobService.isCheckExistsJob(id));
    }

    /**
     * 恢复任务
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/resumeJob")
    @Operation(summary = "恢复任务")
    public Result<Boolean> resumeJob(@RequestParam("id") String id) {
        return Result.success(sysJobService.resumeJob(id));
    }

    /**
     * 暂停任务
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/pauseJob")
    @Operation(summary = "暂停任务")
    public Result<Boolean> pauseJob(@RequestParam("id") String id) {
        return Result.success(sysJobService.pauseJob(id));
    }

}
